package com.bawei.day18_mediaplayer2.utils;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetUtils {
    //移动网络
    public static final int MOBILE_NET=0;
    //无线网络
    public static final int WIFI_NET=1;
    //没有网络
    public static final int NO_NET=2;

    public static int getNetWorkState(Context context) {
//获取连接服务 CONNECTIVITY_SERVICE
        ConnectivityManager connectivityManager = (ConnectivityManager) context

                .getSystemService(Context.CONNECTIVITY_SERVICE);
        //获取当前网络连接
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

        if (activeNetworkInfo != null && activeNetworkInfo.isConnected()) {
            if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_WIFI)) {
                //当前处于无线网络
                return WIFI_NET;
            } else if (activeNetworkInfo.getType() == (ConnectivityManager.TYPE_MOBILE)) {
                //当前处于移动网络
                return MOBILE_NET;
            }
        } else {
            //无网络
            return NO_NET;
        }
        //默认返回  没有网络
        return NO_NET;
    }
}
